<html>
    <head>
        <script type="text/javascript" src="jquery-1.4.1.js"></script>
    </head>

    <body>
        <?php
        require 'debug.php';
        require 'shl.php';

//        echo testFuncs('test1');
        echo testFuncs('test2');
//        echo testFuncs('test3');
//        echo testFuncs('test4');
//        echo testFuncs('test5');
        echo testFuncs('test6');
        echo testFuncs('test7');
        echo testFuncs('test8');
//        echo testFuncs('test_performance_of_json_encode');
        ?>

    </body>
</html>

<?

    function test8(){
        $sh = shCreate('div#div1> /subDiv1 /subDiv2');

        $sh1 = shCreate('div#subdiv1> :this is a sub div 1,, touched:', 1);
        $sh2 = shCreate('div#subdiv2> :this is a sub div 2,, testAttr:~s/testAttr touched:', 0);
        shSet($sh, 'subDiv1', $sh1);
        shSet($sh, 'subDiv2', $sh2);
        $sh2['testAttr'] = 'this is a test attr string';

        return array('test' => 'ok', 'res' => shHtml($sh), 'msg' => 'sub div for template for shl');

    }

        function test_performance_of_json_encode() {
            $a = array('roles' => array(
                    array('name' => 'pop', 'role' => 'Developer', 'roleId' => 2, 'personId' => 12),
                    array('name' => 'pop', 'role' => 'Developer', 'roleId' => 2, 'personId' => 12),
                    array('name' => 'pop', 'role' => 'Developer', 'roleId' => 2, 'personId' => 12),
                    array('name' => 'pop', 'role' => 'Developer', 'roleId' => 2, 'personId' => 12),
                    array('name' => 'pop', 'role' => 'Developer', 'roleId' => 2, 'personId' => 12),
                    array('name' => 'pop', 'role' => 'Developer', 'roleId' => 2, 'personId' => 12),
                ),
                'test' => array(
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                    array('testCaseId' => 1, 'testName' => 'test 1', 'tester' => 'pop', 'testerId' => 12),
                )
            );
            for ($i = 0; $i < 1000; $i++) {
                $s = json_encode($a);
            }
            return array('test' => 'ok', 'res' => $s, 'msg' => 'encode into json');
        }

        function test1() {
            for ($i = 0; $i < 10000; $i++) {
                $s .= $i . '  ';
            }
            return array('test' => 'ok',
                'res' => $s
            );
        }

        function test2() {
            $sh = shCreate('table> tr> ',
                            'th/ :', 'product',
                            'th/ :', 'price',
                            'th/ :', 'provider',
                            '<tr');

            $productList = &$sh['productList'];
            for ($tr = 0; $tr < 1000; $tr++) {
                shPush($sh, 'tr>tr pid:', $tr,
                    'td/ :', 'product #' . $tr,
                    'td/ :', '$' . rand(0, 100),
                    'td/ :', 'provider #' . rand(0, 10)
                        
                );
            }

            $s = shHtml($sh);

            return array('test' => 'ok', 'res' => $s);
        }

        function test3() {
            $sh = shCreate('table>',
                            // table header
                            'tr> ',
                            'th/ :', 'product',
                            'th/ :', 'price',
                            'th/ :', 'provider',
                            '<tr',
                            // table content
                            // data structure productList[]['prodId', 'prodName', 'price', 'providerName']
                            'for(/productList)',
                            'tr>tr prodId:~d/prodId',
                            'td/ :~s/prodName',
                            'td/ :~s/price',
                            'td/ :~s/providerName',
                            '//efor');

            $productList = &$sh['productList'];
            for ($tr = 0; $tr < 1000; $tr++) {
                $productList[] = array(
                    'prodId' => $tr,
                    'prodName' => 'product #' . $tr,
                    'price' => '$' . rand(0, 100),
                    'providerName' => 'provider #' . rand(0, 10)
                );
            }

            $s = shHtml($sh);

            return array('test' => 'ok', 'res' => $s);
        }

        function test4() {
            $sh = shCreate('table>',
                            // table header
                            'tr> ',
                            'th/ :', 'product',
                            'th/ :', 'price',
                            'th/ :', 'provider',
                            '<tr',
                            // table content
                            // data structure productList[]['prodId', 'prodName', 'price', 'providerName']
                            'for(/productList)',
                            'tr>tr prodId:~d/prodId',
                            'td/ :~s/prodName <',
                            '/tpl',
                            'td/ :~s/price',
                            'td/ :~s/providerName',
                            '//efor');

            $productList = &$sh['productList'];
            for ($tr = 0; $tr < 1000; $tr++) {
                $productList[] = array(
                    'prodId' => $tr,
                    'prodName' => 'product #' . $tr,
                    'price' => '$' . rand(0, 100),
                    'providerName' => 'provider #' . rand(0, 10),
                );
            }
            $sh['tpl'] = '<td>' . rand(100, 200) . '</td>';
            $s = shHtml($sh);

            return array('test' => 'ok', 'res' => $s);
        }

        function test5() {
            $sh = shCreate('for(/tables) div> ',
                            // start table
                            'table> ',
                            'tableId: /tableId',
                            'tr> th/ :/header < :what~!,, <',
                            'for(/tableContent)',
                            // if the content is not fixed
                            'if(/isFixed f)',
                            'tr> td/ :/content <tr',
                            '//eif',
                            // if the content is fixed
                            'if(/isFixed t)',
                            'tr> td/ :fixed,, <<',
                            '//eif',
                            '//efor',
                            '<table',
                            'if(~s t) hr/ //eif', 't',
                            '<div',
                            '//efor'
            );

            $tables = &$sh['tables'];
            for ($i = 0; $i < 1; $i++) {
                $table = &$tables[];
                $table['tableId'] = $i;
                $table['header'] = 'content for #' . $i;
                $contents = &$table['tableContent'];
                for ($j = 0; $j < 1000; $j++) {
                    $content = &$contents[];
                    $content['isFixed'] = rand(0, 1) == 1 ? 't' : 'f';
                    $content['content'] = 'unfixed value ' . rand(0, 10);
                }
            }

            $s = shHtml($sh);
            return array('test' => 'ok', 'res' => $s);
        }

        function test6() {
            $sh = shCreate('table> tr> ',
                            'th/ :', 'product',
                            'th/ :', 'price',
                            'th/ :', 'provider',
                            '<tr');

            $productList = &$sh['productList'];
            for ($tr = 0; $tr < 1000; $tr++) {
                shPush($sh, shCreate('tr>tr pid:', $tr,
                                'td/ :', 'product #' . $tr,
                                'td/ :', '$' . rand(0, 100),
                                'td/ :', 'provider #' . rand(0, 10)
                        )
                );
            }

            $s = shHtml($sh);

            return array('test' => 'ok', 'res' => $s, 'msg' => 'The original version');
        }

        function test7() {
            $shObj = shCreate('div>div :div #1,,',
                            'div>#div3 &div #2&',
                            'div#div3> :div #3,, divId:', 3,
                            '<',
                            'div/ :div #4,,',
                            'div/ :div #5,, <',
                            'div> :div #6,, <1',
                            'div> div/ :nested div in #7,, <2',
                            'div.div8> div/ :nested div in #8,, <.div8',
                            'div>.div10|#divNo :div #9,,',
                            'div.div10>table|div :div #10,,',
                            'div> div/ :nested div in #11,, <<'
            );

            $s = shHtml($shObj);
            return array('test' => 'ok', 'res' => $s, 'msg' => 'tag closing');
        }

